// Copyright (c) 2024, the WwMusic project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.springfairy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.springfairy.dao.MoodPointImageMapper;
import com.springfairy.dao.MoodTrackMapper;
import com.springfairy.dao.MoodTrackPointMapper;
import com.springfairy.model.MoodPointImages;
import com.springfairy.model.MoodTrack;
import com.springfairy.model.MoodTrackPoints;
import com.springfairy.service.MoodTrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class MoodTrackServiceImpl implements MoodTrackService {
    @Autowired
    private MoodTrackMapper moodTrackMapper;

    @Autowired
    private MoodTrackPointMapper moodTrackPointMapper;

    @Autowired
    private MoodPointImageMapper moodPointImageMapper;

    @Override
    public MoodTrack createTrack(MoodTrack track) {
        //确保每天只有一条轨迹
        LambdaQueryWrapper<MoodTrack> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MoodTrack::getTrackDate,track.getTrackDate());
        lambdaQueryWrapper.eq(MoodTrack::getUserId,track.getUserId());

        MoodTrack queryMoodTrack = moodTrackMapper.selectOne(lambdaQueryWrapper);
        if(queryMoodTrack != null) {
            return queryMoodTrack;
        }

        track.setId(null);
        try {
            moodTrackMapper.insert(track);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return track;
    }

    @Override
    public MoodTrack createCustomTrack(MoodTrack track) {
        try {
            moodTrackMapper.insert(track);
            return track;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<MoodTrack> getTracksByUser(int userId) {
          return moodTrackMapper.selectList(
                new LambdaQueryWrapper<MoodTrack>()
                        .eq(MoodTrack::getUserId, userId)
                        .orderByDesc(MoodTrack::getTrackDate)
        );
    }

    @Override
    public MoodTrack getTrackById(int id) {
        return moodTrackMapper.selectById(id);
    }

    @Override
    public boolean deleteTrack(int id) {
        return moodTrackMapper.deleteById(id) > 0;
    }

    @Override
    public MoodTrackPoints addTrackPoint(Integer trackId, MoodTrackPoints point) {
        if(trackId == null || point == null) {
            return null;
        }

        MoodTrack moodTrack = moodTrackMapper.selectById(trackId);
        if(moodTrack == null) {
            return null;
        }
        point.setTrackId(moodTrack.getId());
        try {
            moodTrackPointMapper.insert(point);
            return point;
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<MoodTrackPoints> getPointsByTrack(int trackId) {
        return moodTrackPointMapper.selectList(
                new LambdaQueryWrapper<MoodTrackPoints>()
                        .eq(MoodTrackPoints::getTrackId, trackId)
                        .orderByAsc(MoodTrackPoints::getMoodTime)
        );
    }

    @Override
    public boolean deletePoint(int pointId) {
        LambdaQueryWrapper<MoodPointImages> lambdaQueryWrapper = new LambdaQueryWrapper<MoodPointImages>();
        lambdaQueryWrapper.eq(MoodPointImages::getPointId,pointId);

        List<MoodPointImages> images = moodPointImageMapper.selectList(lambdaQueryWrapper);
        String uploadPath = System.getProperty("user.dir") + "/uploads/";

        //删除文件
        for(MoodPointImages image : images) {
            String[] splits = image.getImageUrl().split("/");
            String filename = splits[splits.length - 1];
            File file = new File(uploadPath + filename);
            file.deleteOnExit();
        }

        //删除所有图片
        moodPointImageMapper.delete(lambdaQueryWrapper);
        return moodTrackPointMapper.deleteById(pointId) > 0;
    }

    @Override
    public MoodTrackPoints updatePoint(MoodTrackPoints point) {
        if(moodTrackPointMapper.updateById(point) >= 1) {
            return point;
        }
        return null;
    }

    @Override
    public MoodTrack updateMoodTrack(MoodTrack moodTrack) {
        if( moodTrackMapper.updateById(moodTrack) > 1) {
            return moodTrack;
        } else {
            return null;
        }
    }
}
